package com.qst.rongxiaotong.controller;

import com.qst.rongxiaotong.common.Result;
import com.qst.rongxiaotong.common.StatusCode;
import com.qst.rongxiaotong.pojo.Reserve;
import com.qst.rongxiaotong.service.ReserveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "预约模块管理")
@RequestMapping("/reserve")
@CrossOrigin
public class ReserveController {
    @Autowired
    ReserveService reserveService;

    @ApiOperation("/根据用户查询预约情况")
    @GetMapping("/selectByKind/{kind}")
    public Result selectByKind(@PathVariable("kind")String kind){
        List<Reserve> reserves = reserveService.selectByKind(kind);
        return new Result<>(true, StatusCode.OK,"查询成功",reserves);
    }

    @ApiOperation("/根据ID查询预约")
    @GetMapping("/selectId/{id}")
    public Result selectById(@PathVariable("id")Integer id){
        Reserve reserve = reserveService.selectById(id);
        return new Result<>(true,StatusCode.OK,"查询成功",reserve);
    }
    @ApiOperation("/根据ID删除预约")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable("id")Integer id){
        int row = reserveService.deleteById(id);
        if(row!=1){
            return new Result<>(true,StatusCode.OK,"删除成功");
        }
        return new Result<>(false,StatusCode.ERROR,"删除失败");
    }

    @ApiOperation("根据ID修改预约")
    @PutMapping("/update/")
    public Result updateById(@RequestBody Reserve reserve){
        reserveService.updateById(reserve);
        return new Result<>(true,StatusCode.OK,"更新成功",reserve);
    }
    @ApiOperation("/添加预约信息")
    @PostMapping("/addReserve")
    public Result addReserve(@RequestBody Reserve reserve){
        reserveService.addReserve(reserve);
        System.out.println(reserve);
        return new Result<>(true,StatusCode.OK,"添加成功",reserve);

    }
    @ApiOperation(value = "添加预约情报")
    @PostMapping("/add")
    public Result add(@RequestBody Reserve reserve, BindingResult bindingResult){
        //检查项
        if (bindingResult.hasErrors()){
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors){
                stringBuffer.append(objectError.getDefaultMessage()).append(";");
            }
            String string = stringBuffer.toString();
            System.out.println(string);
            Result<String> result = new Result<>(false, StatusCode.ERROR, "添加失败", string);
            return result;
        }
        reserveService.insert(reserve);
        Result<String> result = new Result<>(false, StatusCode.ERROR, "成功");
        return result;
    }

}
